//种子列表
var _seedList = [
    {
        name: "甘蓝"
    },
    {
        name: "马铃薯",
    },
    {
        name: "胡萝卜",
    },
    {
        name: "青椒",
    },
    {
        name: "菠菜",
    },
    {
        name: "萝卜"
    },
    {
        name: "白萝卜"
    },
    {
        name: "水萝卜"
    },
    {
        name: "哈密瓜"
    },
    {
        name: "洋葱"
    },
    {
        name: "南瓜"
    },
    {
        name: "黄瓜"
    },
    {
        name: "玉米"
    },
    {
        name: "西红柿"
    },
    {
        name: "茄子"
    },
    {
        name: "地瓜"
    },
    {
        name: "大葱"
    },
    {
        name: "白菜"
    },
    {
        name: "草莓"
    },
    {
        name: "菠萝"
    },
    {
        name: "牧草"
    }

];

var _plantList = [
    {
        name: "甘蓝"
    },
    {
        name: "马铃薯",
    },
    {
        name: "胡萝卜",
    },
    {
        name: "青椒",
    },
    {
        name: "菠菜",
    },
    {
        name: "萝卜"
    },
    {
        name: "白萝卜"
    },
    {
        name: "水萝卜"
    },
    {
        name: "哈密瓜"
    },
    {
        name: "洋葱"
    },
    {
        name: "南瓜"
    },
    {
        name: "黄瓜"
    },
    {
        name: "玉米"
    },
    {
        name: "西红柿"
    },
    {
        name: "茄子"
    },
    {
        name: "地瓜"
    },
    {
        name: "大葱"
    },
    {
        name: "白菜"
    },
    {
        name: "草莓"
    },
    {
        name: "菠萝"
    },
    {
        name: "牧草"
    }
];

//存档
var _archive = {
    //土地记录
    grounds: [],
    //背包
    bag: [],
};


//加载存档
function _load() {
    var res = cc.sys.localStorage.getItem('archive');

    if (res) {
        _archive = JSON.parse(res);
        return res;
    } else {
        return _archive;
    }
}
//保存存档
function _save() {

    cc.sys.localStorage.setItem('archive', JSON.stringify(_archive));
}

/*********************尝试组织数据 风格1*********** */
//设置土地
function _setGround(index, userData) {
    _archive.grounds[index] = userData;
    _save();//直接保存
}
//获取土地
function _getGround(index) {

    var ground = _archive.grounds[index];

    //初始化土地信息
    if (!ground) {
        ground = {
            type: 0,
            index: index
        };
        // _setGround(index, ground);
    }

    return ground;
}

/*******************尝试组织数据 风格2*************** */
//设置背包内容
function _setBagItem(type, count) {

    var handledItem = false;

    for (var i = 0; i < _archive.bag.length; i++) {
        var item = _archive.bag[i];

        //找到了同样的item
        if (item.type === type) {
            item.count += count;
            handledItem = true;
            break;
        }
    }

    //如果未找到同样的 直接写入item里
    if (!handledItem) {
        _archive.bag[_archive.bag.length] = {
            type: type,
            count: count
        }
    }
    //保存
    _save();
}

//读取背包内容
function _getBagItem(index) {
    var bagItem = _archive.bag[index];
    return bagItem;
}

/************************
 * 导出模块
 ***********************/
module.exports = {
    //常量
    bagLength:50,//50格背包
    
    seedList: _seedList,
    plantList: _plantList,
    archive: _archive,
    load: _load,
    save: _save,
    getGround: _getGround,
    setGround: _setGround,
    getBagItem: _getBagItem,
    setBagItem: _setBagItem,

}
